package com.lli.mc.converter.jave;

import it.sauronsoftware.jave.AudioAttributes;
import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.EncoderException;
import it.sauronsoftware.jave.EncodingAttributes;
import it.sauronsoftware.jave.InputFormatException;

import java.io.File;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 音频转换器
 * 
 * @author lli
 *
 * @version 1.0
 *
 */
public class AudioConverter {
    private static final Logger log = LoggerFactory
            .getLogger(AudioConverter.class);

    /**
     * 
     * @description 其他类型转mp3
     * @author lli
     * @create 2017年7月25日下午1:50:28
     * @version 1.0
     * @param sourcePath
     * @param targetPath
     */
    public static void changeToMp3(File source, File target) {
        log.info("音频转换开始！");
        long start = System.currentTimeMillis();

        AudioAttributes audio = new AudioAttributes();
        Encoder encoder = new Encoder();
        audio.setCodec("libmp3lame");
        EncodingAttributes attrs = new EncodingAttributes();
        attrs.setFormat("mp3");
        attrs.setAudioAttributes(audio);

        try {
            encoder.encode(source, target, attrs);
        } catch (IllegalArgumentException e) {
            log.error("", e);
        } catch (InputFormatException e) {
            log.error("", e);
        } catch (EncoderException e) {
            // e.printStackTrace();
        }
        log.info("音频转换结束！原始文件：{}，转换后文件：{}，耗时：{}ms", source.getPath(),
                target.getPath(), System.currentTimeMillis() - start);
    }
}